package com.ilovepostcard;

import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.ilovepostcard.controller.OffLineManager;
import com.ilovepostcard.controller.UserController;
import com.ilovepostcard.utils.Constants;
import com.ilovepostcard.utils.DrawableManager;
import com.ilovepostcard.utils.Utils;
import com.mobclick.android.MobclickAgent;

public class MenuActivity extends BaseActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.options_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.check_update:
            Utils.checkForUpdate(this,true);
            break;
        case R.id.logout:
        	MobclickAgent.onEvent(this, Constants.COUNT_MENU_LOGOUT);
            UserController.logout(this);
            Intent intent = new Intent(this, HomeActivity.class);
            startActivity(intent);
            Toast.makeText(this, R.string.success_logout, Toast.LENGTH_SHORT).show();
            DrawableManager.getInstance().clearAllFileCache();
            OffLineManager.getInstance().deleteAll();
            intent = new Intent("com.ilovepostcard.Exist");
            sendBroadcast(intent);
            break;
        case R.id.about:
            Intent about = new Intent(this, AboutActivity.class);
            startActivity(about);
            break;
        case R.id.addfriend:
        	MobclickAgent.onEvent(this, Constants.COUNT_MENU_INVITE);
            Utils.inviteFriends(this);
            break;
        case R.id.exit:
            doExist();
            break;
        case R.id.feedback:
        	MobclickAgent.onEvent(this, Constants.COUNT_MENU_FEEDBACK);
            Intent feedback = new Intent(this, FeedbackActivity.class);
            startActivity(feedback);
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void doExist() {
        Utils.showConfirmDialog(this, getString(R.string.quit_confirm), new Runnable() {

            @Override
            public void run() {
                Intent intent = new Intent("com.ilovepostcard.Exist");
                sendBroadcast(intent);
            }
        }, null);
    }

}
